class ShipownersController < ApplicationController
  # GET /shipowners
  # GET /shipowners.json
  def index
    @shipowners = Shipowner.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @shipowners }
    end
  end

  # GET /shipowners/1
  # GET /shipowners/1.json
  def show
    @shipowner = Shipowner.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @shipowner }
    end
  end

  # GET /shipowners/new
  # GET /shipowners/new.json
  def new
    @shipowner = Shipowner.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @shipowner }
    end
  end

  # GET /shipowners/1/edit
  def edit
    @shipowner = Shipowner.find(params[:id])
  end

  # POST /shipowners
  # POST /shipowners.json
  def create
    @shipowner = Shipowner.new(params[:shipowner])

    respond_to do |format|
      if @shipowner.save
        format.html { redirect_to @shipowner, notice: 'Shipowner was successfully created.' }
        format.json { render json: @shipowner, status: :created, location: @shipowner }
      else
        format.html { render action: "new" }
        format.json { render json: @shipowner.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /shipowners/1
  # PUT /shipowners/1.json
  def update
    @shipowner = Shipowner.find(params[:id])

    respond_to do |format|
      if @shipowner.update_attributes(params[:shipowner])
        format.html { redirect_to @shipowner, notice: 'Shipowner was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @shipowner.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /shipowners/1
  # DELETE /shipowners/1.json
  def destroy
    @shipowner = Shipowner.find(params[:id])
    @shipowner.destroy

    respond_to do |format|
      format.html { redirect_to shipowners_url }
      format.json { head :ok }
    end
  end


  # POST /shipowners/1/receive_datagram
  def receive_datagram

    shipowner_data = ActiveSupport::JSON.decode params[:data]
    logger.debug shipowner_data
    shipowner = Shipowner.find_by_code("#{params[:id]}")
    shipowner.datagram_save(shipowner_data, params[:id])
    shipowner.read_datagram(shipowner_data)
    render :text => '200'
  end

  skip_before_filter :require_login, :only => :receive_datagram
end
